﻿using System;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;

class GameplayScreen : GameScreen
{
    ContentManager content;

    HUD hud;
    public HUD Hud
    {
        get { return hud; }
        set { hud = value; }
    }

    private bool startRandom;

    public GameplayScreen(PlayerAndChar pla)
    {
		Globals.Players = pla;

        startRandom = false;

        TransitionOnTime = TimeSpan.FromSeconds(1.5);
        TransitionOffTime = TimeSpan.FromSeconds(0.1);
    }

    public override void LoadContent()
    {
        content = ScreenManager.Game.Content;

        GameSession.StartNewSession(ScreenManager, this, new EventManager(content), new PuzzleManager(content));

        GameSession.LoadContent(content);

        // A real game would probably have more content than this sample, so
        // it would take longer to load. We simulate that by delaying for a
        // while, giving you a chance to admire the beautiful loading screen.
        //Thread.Sleep(1000);

        // once the load has finished, we use ResetElapsedTime to tell the game's
        // timing mechanism that we have just finished a very long frame, and that
        // it should not try to catch up.
        ScreenManager.Game.ResetElapsedTime();
    }

    public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
    {
        base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

       	if (!Globals.IsPaused)
       	{
        	GameSession.Update(gameTime);
		}
    }

    public override void HandleInput()
    {
        if (InputManager.IsActionTriggered(InputManager.Action.Pause))
        {
            //pause
            ScreenManager.AddScreen(new PauseScreen());
        }
    }

    public override void UnloadContent()
    {
        startRandom = false;

        base.UnloadContent();
    }

    public override void Draw(GameTime gameTime)
    {
        SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

        spriteBatch.Begin();

        int ytile = 0, xtile = 0;

        for (int i = 0; i < 20; i++)
            for (int j = 0; j < 13; j++)
            {
                if (j == 12)
                {
                    ytile = GameSession.SceneryMatriz[i, j-1] / (GameSession.TileSize);
                    xtile = GameSession.SceneryMatriz[i, j-1] - ytile * (GameSession.TileSize);
                    spriteBatch.Draw(GameSession.Tiles, new Vector2(i * 64, j * 64 + GameSession.screenAdjustment), new Rectangle(xtile * 64, ytile * 64, 64, 64), Color.White);

                    continue;
                }

                ytile = GameSession.SceneryMatriz[i, j] / (GameSession.TileSize);
                xtile = GameSession.SceneryMatriz[i, j] - ytile * (GameSession.TileSize);
                spriteBatch.Draw(GameSession.Tiles, new Vector2(i * 64, j * 64 + GameSession.screenAdjustment), new Rectangle(xtile * 64, ytile * 64, 64, 64), Color.White);
            }

        //draw back objects
        foreach (Objects obj in GameSession.ObjectsList)
        {
            if (obj.Kind != ObjectKind.Barrel && obj.Kind != ObjectKind.PushingStone)
            {
            
                if (obj.GetType() == typeof(PuzzleObject))
                {

                    //Console.WriteLine(obj.GetType());
                    PuzzleObject puzzleObj = (PuzzleObject)obj;
                    ytile = puzzleObj.PaintNumber / (GameSession.ObjectSize);
                    xtile = puzzleObj.PaintNumber - ytile * (GameSession.ObjectSize);
                }
                else
                {
                    if (obj.GetType() == typeof(Trap))
                    {
                        Trap trap = (Trap)obj;
                        ytile = trap.PaintNumber / (GameSession.ObjectSize);
                        xtile = trap.PaintNumber - ytile * (GameSession.ObjectSize);
                    }
                    else
                    {

                        ytile = obj.PaintNumber / (GameSession.ObjectSize);
                        xtile = obj.PaintNumber - ytile * (GameSession.ObjectSize);
                    }
                }
                spriteBatch.Draw(GameSession.Objects, new Vector2(obj.XPosition * 64, obj.YPosition * 64 + GameSession.screenAdjustment), new Rectangle(xtile * 64, ytile * 64, 64, 64), Color.White);
                
            }
        }


        //draw front objects
        foreach (Objects obj in GameSession.ObjectsList)
        {
            if (obj.Kind == ObjectKind.Barrel || obj.Kind == ObjectKind.PushingStone)
            { 
                if (obj.GetType() == typeof(PuzzleObject))
                {
                    PuzzleObject puzzleObj = (PuzzleObject)obj;

                    ytile = puzzleObj.PaintNumber / (GameSession.ObjectSize);
                    xtile = puzzleObj.PaintNumber - ytile * (GameSession.ObjectSize);

                    puzzleObj.Draw(xtile, ytile, spriteBatch);
                }
                else
                {
                    ytile = obj.PaintNumber / (GameSession.ObjectSize);
                    xtile = obj.PaintNumber - ytile * (GameSession.ObjectSize);
                    spriteBatch.Draw(GameSession.Objects, new Vector2(obj.XPosition * 64, obj.YPosition * 64 + GameSession.screenAdjustment), new Rectangle(xtile * 64, ytile * 64, 64, 64), Color.White);   

                }
            }

            if (obj.GetType() == typeof(Tower))
            {
                Tower tower = (Tower)obj;
                foreach (Shot shot in tower.ShotList)
                {
                    shot.Draw(gameTime, spriteBatch);

                }
            }
        }

        spriteBatch.End();


        hud.Draw(Globals.Players);

        GameSession.Draw(gameTime);
    }
}
